﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Runtime.Serialization;
using System.Web.UI.WebControls;
using ApplicationGenerator.BusinessModule.Elements;
using ApplicationGenerator.CommonModule;
using ApplicationGenerator.Forms.Actions;
using ApplicationGenerator.Forms.Forms;
using ApplicationGenerator.Forms.Interfaces;
using ApplicationGenerator.BusinessModule.Interfaces;
using System.ComponentModel.Design;
using ApplicationGenerator.CommonForms;
using ApplicationGenerator.Versions.Forms;
using ApplicationGenerator.Versions.BusinessLogic;
using System.Collections.Generic;

namespace ApplicationGenerator.Forms.WebControls
{
    /// <summary>
    /// Kontrolka przycisku, pozwalająca na wykonanie akcji na formularzu
    /// </summary>
    [ToolboxBitmap(typeof(Button))]
    [DefaultProperty("ControlAction")]
    [Serializable]
    public class ButtonControl : BaseControl, IInitializeControl, IWebServiceInvocation, ICustomTypeDescriptor
    {
        #region Fields
        /// <summary>
        /// Webowa kontrolka przycisku
        /// </summary>
        [NonSerialized]
        private Button _button;

        /// <summary>
        /// Typ wykonywanej akcji
        /// </summary>
        private ActionBase _controlAction;

        #endregion

        #region Properties
        /// <summary>
        /// Typ wykonywanej akcji
        /// </summary>
        [Category("Rozszerzone dane")]
        [DisplayName("Typ akcji")]
        [Description("Akca powiązana z daną kontrolką.")]
        [Editor(typeof(ButtonActionTypeEditor), typeof(UITypeEditor))]
        [TypeConverter(typeof(ButtonActionTypeConverter))]
        public ActionBase ControlAction
        {
            get
            {
                return _controlAction;
            }

            set
            {
                _controlAction = value;
                TypeDescriptor.Refresh(this);
            }


        }

        /// <summary>
        /// Tekst wyświetlany na przycisku
        /// </summary>
        [Category("Rozszerzone dane")]
        [DisplayName("Tekst")]
        [Description("Tekst, który ma zostać wyświetlony na kontrolce.")]
        public string Text
        {
            get;
            set;
        }

        /// <summary>
        /// Kontrolka webowa powiązana z daną kontrolką
        /// </summary>
        [Browsable(false)]
        public override WebControl Control
        {
            get
            {
                if (_button == null)
                {
                    _button = new Button();
                }

                return _button;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Wykonanie akcji
        /// </summary>
        /// <param name="formWebControl">Kontrolka webowa formularza</param>
        public void ExecuteAction(FormWebControl formWebControl)
        {
            ControlAction.Execute(this, formWebControl);
        }

        #endregion

        #region IValidation Members
        /// <summary>
        /// Waliduje encję
        /// </summary>
        /// <param name="errorList">Lista błędów</param>
        /// <returns>Czy encja jest poprawna ?</returns>
        public override bool IsValid(ErrorEntity errorList)
        {
            bool isValid = base.IsValid(errorList);
            if (ControlAction == null)
            {
                errorList.ErrorStrings.Add("Należy wybrać akcję dla kontrolki.");
                isValid = false;
            }

            return isValid;
        }

        #endregion

        #region IWebServiceInvocation Members

        /// <summary>
        /// Adres URL web serwisu
        /// </summary>
        [Category("Rozszerzone dane")]
        [DisplayName("Adres URL")]
        [Description("Adres URL web serwisu, który ma zostać wywołany")]
        [Editor(typeof(MultilineStringEditor), typeof(UITypeEditor))]
        public string WebServiceURL
        {
            get;
            set;
        }

        /// <summary>
        /// Nazwa metody do wykonania
        /// </summary>
        [Category("Rozszerzone dane")]
        [DisplayName("Nazwa metody")]
        [Description("Nazwa metody web serwisu, która ma zostać wywołana")]
        [Editor(typeof(MethodNameTypeEditor), typeof(UITypeEditor))]
        public string MethodName
        {
            get;
            set;
        }

        /// <summary>
        /// Mapowania parametrów
        /// </summary>
        [Category("Rozszerzone dane")]
        [DisplayName("Mapowania parametrów")]
        [Description("Mapowania pól ze struktury danych na parametry metody")]
        [Editor(typeof(MappingsEditor), typeof(UITypeEditor))]
        [TypeConverter(typeof(MappingsConverter))]
        public byte[] MappingsConfiguration
        {
            get;
            set;
        }

        /// <summary>
        /// Nazwa meta modelu, na podstawie którego będą wybierane mapowania
        /// </summary>
        [Browsable(false)]
        public string MetaModelName
        {
            get
            {
                if (MetaModel == null)
                {
                    return null;
                }

                return MetaModel.Name;
            }
        }

        /// <summary>
        /// Wersja modelu, na której oparty jest formularz
        /// </summary>
        [Category("Rozszerzone dane")]
        [DisplayName("Model")]
        [Description("Model, z którego wybierane będą pola do mapowania na parametry metody web serwisu")]
        [TypeConverter(typeof(VersionConverter))]
        [Editor(typeof(VersionEditor), typeof(UITypeEditor))]
        [VersionType(VersionElement.Model)]
        public VersionInfo MetaModel
        {
            get;
            set;
        }
        #endregion

        #region IInitializeControl Members

        /// <summary>
        /// Inicjalizuje kontrolkę
        /// </summary>
        public void Initialize()
        {
            _button.Text = Text;
        }

        #endregion

        #region ICustomTypeDescriptor Members

        public AttributeCollection GetAttributes()
        {
            return TypeDescriptor.GetAttributes(this, true);
        }

        public string GetClassName()
        {
            return TypeDescriptor.GetClassName(this, true);
        }

        public string GetComponentName()
        {
            return TypeDescriptor.GetComponentName(this, true);
        }

        public TypeConverter GetConverter()
        {
            return TypeDescriptor.GetConverter(this, true);
        }

        public EventDescriptor GetDefaultEvent()
        {
            return TypeDescriptor.GetDefaultEvent(this, true);
        }

        public PropertyDescriptor GetDefaultProperty()
        {
            return TypeDescriptor.GetDefaultProperty(this, true);
        }

        public object GetEditor(Type editorBaseType)
        {
            return TypeDescriptor.GetEditor(this, editorBaseType, true);
        }

        public EventDescriptorCollection GetEvents()
        {
            return TypeDescriptor.GetEvents(this, true);
        }

        public EventDescriptorCollection GetEvents(Attribute[] attributes)
        {
            return TypeDescriptor.GetEvents(this, attributes, true);
        }

        public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            PropertyDescriptorCollection colAvailableCollection = TypeDescriptor.GetProperties(this, attributes, true);
            List<PropertyDescriptor> colDisplayProperties = new List<PropertyDescriptor>();
            colDisplayProperties.Add(colAvailableCollection["BussinessId"]);
            colDisplayProperties.Add(colAvailableCollection["ControlAction"]);
            colDisplayProperties.Add(colAvailableCollection["Text"]);
            if (ControlAction is InvokeWebServiceAction)
            {
                colDisplayProperties.Add(colAvailableCollection["WebServiceURL"]);
                colDisplayProperties.Add(colAvailableCollection["MethodName"]);
                colDisplayProperties.Add(colAvailableCollection["MappingsConfiguration"]);
                colDisplayProperties.Add(colAvailableCollection["MetaModel"]);
            }

            return new PropertyDescriptorCollection(colDisplayProperties.ToArray());
        }

        public PropertyDescriptorCollection GetProperties()
        {
            return TypeDescriptor.GetProperties(this, true);
        }

        public object GetPropertyOwner(PropertyDescriptor pd)
        {
            return this;
        }

        #endregion

    }
}
